﻿using System;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OracleClient;

namespace Utility.Database
{
    public class Oracle
    {
        #region 构造函数
        public Oracle()
        {
        }
        #endregion

        #region 非事务控制
        #region ExecuteOracleCommand
        /// <summary>
        /// Oracle数据库的Sql命令执行
        /// </summary>
        /// <param name="oracleConnectionString">Oracle数据库的连接字符串</param>
        /// <param name="oracleCommandText">数据库执行的Sql命令</param>
        public static void ExecuteOracleCommand(string oracleConnectionString, string oracleCommandText)
        {
            OracleConnection oracleConnection = new OracleConnection(oracleConnectionString);
            OracleCommand oracleCommand = new OracleCommand(oracleCommandText, oracleConnection);
            try
            {
                oracleConnection.Open();
                oracleCommand.ExecuteNonQuery();
                oracleConnection.Close();
            }
            catch (Exception exp)
            {
                throw exp;
            }
        }
        #endregion

        #region ExecuteOracleSelection
        /// <summary>
        /// Oracle数据库的Sql查询命令执行
        /// </summary>
        /// <param name="oracleConnectionString">Oracle数据库的连接字符串</param>
        /// <param name="oracleSelectCommandText">Sql查询命令</param>
        public static DataTable ExecuteOracleSelection(string oracleConnectionString, string oracleSelectCommandText)
        {
            OracleConnection oracleConnection = new OracleConnection(oracleConnectionString);
            OracleDataAdapter oracleDataAdapter = new OracleDataAdapter(oracleSelectCommandText, oracleConnection);
            DataTable dataTableReturn = new DataTable();
            try
            {
                oracleConnection.Open();
                oracleDataAdapter.Fill(dataTableReturn);
                oracleConnection.Close();
                return dataTableReturn;
            }
            catch (Exception exp)
            {
                throw exp;
            }
        }
        #endregion
        #endregion

        #region 事务控制
        #region ExecuteOracleCommand
        public static void ExecuteOracleCommand(OracleTransaction oracleTransaction, string oracleCommandText)
        {
            OracleCommand oracleCommand = new OracleCommand(oracleCommandText, oracleTransaction.Connection);
            try
            {
                oracleCommand.ExecuteNonQuery();
            }
            catch (Exception exp)
            {
                throw exp;
            }
        }

        public static void ExecuteOracleCommand(string oracleConnectionString, ArrayList oracleCommandTextList)
        {
            OracleConnection oracleConnection = new OracleConnection(oracleConnectionString);
            try
            {
                oracleConnection.Open();
                OracleTransaction oracleTransaction = oracleConnection.BeginTransaction();
                try
                {
                    for (int i = 0; i < oracleCommandTextList.Count; i++)
                    {
                        using (OracleCommand oracleCommand = new OracleCommand(oracleCommandTextList[i].ToString(), oracleConnection))
                        {
                            oracleCommand.Transaction = oracleTransaction;
                            oracleCommand.ExecuteNonQuery();
                        }
                    }
                    oracleTransaction.Commit();
                }
                catch (Exception exp)
                {
                    oracleTransaction.Rollback();
                    throw exp;
                }
            }
            catch (Exception exp)
            {
                throw exp;
            }
        }
        #endregion
        #endregion
    }
}
